<?php 
ini_set('date.timezone','Asia/Shanghai');  
//error_reporting(E_ERROR);  
require_once "../app/Wechat/lib/WxPay.Api.php";  
require_once "../app/Wechat/example/WxPay.JsApiPay.php";  
require_once '../app/Wechat/example/log.php';  

//①、获取用户openid  
$tools = new JsApiPay();  
  
$openId = $tools->GetOpenid("/wechat?money=".$money); //传参要这样传，还要改一下example/WxPay.JsApiPay.php文件  
  
//②、统一下单  
$input = new WxPayUnifiedOrder();  
$input->SetBody("微信支付");  
$input->SetAttach("微信支付");  
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));  
$input->SetTotal_fee($money);  
$input->SetTime_start(date("YmdHis"));  
$input->SetTime_expire(date("YmdHis", time() + 600));  
$input->SetGoods_tag("");  
$input->SetNotify_url("http://aaa.com/wechat/notify");//这里填你的回调路径，是绝对路径，例如http://aaa.com/wechat/notify  
$input->SetTrade_type("JSAPI");  
$input->SetOpenid($openId);  
$order = WxPayApi::unifiedOrder($input);  
// echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';  
// printf_info($order);  
$jsApiParameters = $tools->GetJsApiParameters($order);  
  
//获取共享收货地址js函数参数  
$editAddress = $tools->GetEditAddressParameters();  
  
//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php  
/** 
 * 注意： 
 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功 
 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”， 
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html） 
 */  
  
  
?>  
  
<html>  
<head>  
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>  
    <meta name="viewport" content="width=device-width, initial-scale=1"/>   
    <title>微信支付</title>  
  
    <script src="http://7xwdxi.com1.z0.glb.clouddn.com/lib/js/jquery-2.0.3.min.js"></script>  
      
    <script type="text/javascript">  
    //调用微信JS api 支付  
    function jsApiCall()  
    {  
        WeixinJSBridge.invoke(  
            'getBrandWCPayRequest',  
            <?php echo $jsApiParameters; ?>,  
            function(res){      
                                //这底下的东西就是上面$input->SetNotify_url("http://aaa.com/notify");这个传过来的  
                WeixinJSBridge.log(res.err_msg);  
                // alert(res.err_code+res.err_desc+res.err_msg);  
                if (res.err_msg == "get_brand_wcpay_request:ok") { //如果微信支付成功  
                    // message: "微信支付成功!"  
                    alert("支付成功！");  
                    window.location.href="/wechat/do";//你成功后要跳转的页面或控制器
                }else if(res.err_msg == "get_brand_wcpay_request:cancel"){ //如果取消微信支付  
                    alert("您已取消支付");  
                      
                }  
            }  
        );  
    }  
  
    function callpay()  
    {  
        if (typeof WeixinJSBridge == "undefined"){  
            if( document.addEventListener ){  
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);  
            }else if (document.attachEvent){  
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);   
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);  
            }  
        }else{  
            jsApiCall();  
        }  
    }  
    </script>  
    <script type="text/javascript">  
    //获取共享地址  
    function editAddress()  
    {  
        WeixinJSBridge.invoke(  
            'editAddress',  
            <?php echo $editAddress; ?>,  
            function(res){  
                var value1 = res.proviceFirstStageName;  
                var value2 = res.addressCitySecondStageName;  
                var value3 = res.addressCountiesThirdStageName;  
                var value4 = res.addressDetailInfo;  
                var tel = res.telNumber;  
                  
                alert(value1 + value2 + value3 + value4 + ":" + tel);  
            }  
        );  
    }  
      
    window.onload = function(){  
        if (typeof WeixinJSBridge == "undefined"){  
            if( document.addEventListener ){  
                document.addEventListener('WeixinJSBridgeReady', editAddress, false);  
            }else if (document.attachEvent){  
                document.attachEvent('WeixinJSBridgeReady', editAddress);   
                document.attachEvent('onWeixinJSBridgeReady', editAddress);  
            }  
        }else{  
            editAddress();  
        }  
    };  
      
    </script>  
  
  
</head>  
<body>  
    <div class="title-bar">  
        <span><a href="/home/student/pay"><img src="http://7xwdxi.com1.z0.glb.clouddn.com/images/back-icon.png"></a></span>  
        <h1>充值</h1>  
    </div>  
    <br/>  
    <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px"><?php $sum=$money/100; ?>{{$sum}}</span>元钱</b></font><br/><br/>  
      
    <div align="center">  
        <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" type="button" onclick="callpay()" >立即支付</button>  
  
    </div>  
  
</body>  
</html>  